﻿using System;
using System.Windows;
using System.Threading;

namespace TravellerApp1.Views
{
    /// <summary>
    /// CreateDb.xaml 的交互逻辑
    /// </summary>
    public partial class CreateDb : Window
    {
        public CreateDb()
        {
            InitializeComponent();
            if(MysqlHelper.DatabaseExist("travel947"))
            {
                //MessageBox.Show("exist");
                MysqlHelper.SetDatabase("travel947");
                Thread.Sleep(10);
                GotoNextWindow(this);
            }
            else
            {
                //MessageBox.Show("not exist");
                //this.Visibility = Visibility.Visible;
                MysqlHelper.CreateDatabase("travel947");
                MysqlHelper.SetDatabase("travel947");
                string str1 = "创建数据库travel947 " + DateTime.Now.ToLocalTime().ToString();
                int cnt = MysqlHelper.ExecuteSqlScript("sqlscript1.sql", 1);
                str1 += Environment.NewLine + "创建相关数据表，共计" + cnt.ToString() + "条语句 " + DateTime.Now.ToLocalTime().ToString();
                MessageBox.Show(str1, "首次运行");
                Thread.Sleep(20);
                GotoNextWindow(this);
            }
        }

        public static void GotoNextWindow(CreateDb current)
        {
            UserorAdmin login1 = new();
            login1.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            login1.Show();
            WinHelper.ForceClose(current);
        }
    }
}
